﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.ServiceModel;
using System.Web.UI.WebControls;
using ProductServiceReference;
using OrderServiceReference;
using AccountServiceReference;

public partial class UserControls_BuyProduct : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        var client = new OrderServiceClient();
        foreach (var method in client.GetMethods())
        {
            var lstItem = new ListItem(method.MethodName, method.MethodID + "");
           ddlMethod.Items.Add(lstItem);
        }
        MultiView1.ActiveViewIndex = 0;
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        try
        {
            var client = new OrderServiceClient();
            var accountid = new AccountServiceClient().GetAccountId(Session["User"].ToString());
            var result = client.AddOrder(new Order()
                                             {
                                                 AccountID = accountid,
                                                 CreditCardNumber = txtCreditNumber.Text
                                                             ,
                                                 NewAddress = txtNewAddress.Text.Trim(),
                                                 Quantity = int.Parse(txtquantity.Text.Trim()),
                                                 ProductID = int.Parse(Request.QueryString["proid"]),
                                                 PaymentMethodID = int.Parse(ddlMethod.SelectedValue)
                                             });
            if(result)
            {
                MultiView1.ActiveViewIndex = 1;
            }else
            {
                lblmsg.Text = "Buy product not success!";
            }
        }
        catch (FaultException<Account> faultException)
        {
            lblmsg.Text = faultException.Message;
        }
        catch (FaultException<Order> faultException)
        {
            lblmsg.Text = faultException.Message;
        }
        catch (Exception ex)
        {
            lblmsg.Text = "Ban chua dang nhap!";

        }
    }

    protected void ddlmethod_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlMethod.SelectedValue.Equals("2"))
        {
            txtCreditNumber.Enabled = true;
        }
    }
}